import React, { Component } from 'react';
import { toJS } from 'mobx';
import { observer, inject } from 'mobx-react';
import { withRouter } from 'react-router-dom';
import { TabBar, Icon, ActionSheet, Tools, Modal } from 'weaver-mobile';
import { doConfirm } from '../../util';

const getLabel = Tools.getLabel;

@inject('viewStore')
@observer
@withRouter
export default class ViewButtons extends Component {
  static defaultProps = {
    prefixCls: 'email-view-bar',
  }

  onPress = (d) => {
    const { history, updateListData, viewStore, from, needRefresh, refreshList, delCallBack } = this.props;
    const { params: { mailId }, path, secId, deleteEmail, recall, docs, cusBrowser, setNewParams, mailUserSetting, setState } = viewStore;
    const deleteCallBack = () => {
      if (delCallBack && typeof delCallBack === 'function') {
        delCallBack(d.key, mailId)
      } else if (!delCallBack && updateListData && typeof updateListData === 'function') {
        updateListData(mailId, {}, true);
      }
      history.go(-1);
    }
    if (d.rows && d.rows.length > 0) {
      this.showActionSheet(d.rows);
    } else {
      switch (d.key) {
        case 'delete': // 删除
          let modal = doConfirm(getLabel('15097','确定要删除吗？'), '', () => {
            let callBack = from && from === 'attachment' ? () => { history.go(-1) } : deleteCallBack
            deleteEmail({ operate: 'delete', mailIds: mailId }, callBack);
          });
          setState({currentModal: modal})
          break;
        case 'remove': // 永久删除
          modal = doConfirm(getLabel('15097','确定要删除吗？'), '', () => {
            let callBack = from && from === 'attachment' && needRefresh ? () => { refreshList(); history.go(-1) } : deleteCallBack
            deleteEmail({ operate: 'deletePermanently', mailIds: mailId }, callBack);
          });
          setState({currentModal: modal})
          break;
        case 'exportToDoc': // 导出文档
          if (secId > 0) {
            let docTip = toJS(mailUserSetting) && toJS(mailUserSetting).docRight ? 
              toJS(mailUserSetting).secMap && `${getLabel('512946','当前导出文档目录为：')}${toJS(mailUserSetting) && toJS(mailUserSetting).secMap && toJS(mailUserSetting).secMap.valueSpan}` : 
              getLabel('512947','当前导出文档目录无权限操作，请更换导出目录！') ;
            let docBtns = [
              { text: getLabel('512948','更换目录'), onPress: () => {history.push(`${path}/doc`)} },
            ];
            if (toJS(mailUserSetting) && toJS(mailUserSetting).docRight) {
              docBtns.push({ text: getLabel('17416','导出'), disabled: true, onPress: () => {docs({ mailIds: mailId, secId })} })
            }
            Modal.alert(docTip,'', docBtns);
          } else {
            history.push(`${path}/doc`);
          }
          break;
        case 'exportToCustomerContact': // 导出客户联系
          // if (crmSecId > 0) {
          //   crmContactRecord({ mailIds: mailId, crmIds: crmSecId });
          // } else {
            history.push(cusBrowser.path);
          // }
          break;
        case 'topRecall': // 撤回
          modal = doConfirm(getLabel(505366, '撤回操作不可恢复，确定要撤回已发送邮件吗？'), '', () => {
            recall({ mailId });
          });
          setState({currentModal: modal})
          break;
        case 'topMarkAs': // 标记为
          history.push(`${path}/setTag`);
          break;
        case 'topMoveTo': // 移动到
          history.push(`${path}/moveTo`);
          break;
        case 'towaitDeal': // 设置待办
          history.push(`${path}/todo`);
          break;
        case 'reply': // 回复
          setNewParams(1, () => history.push(`${path}/new`));
          break;
        case 'ReplyAll': // 回复全部
          setNewParams(2, () => history.push(`${path}/new`));
          break;
        case 'topForward': // 转发
          setNewParams(3, () => history.push(`${path}/new`));
          break;
        case 'topEditAgain': 
          setNewParams(5, () => history.push(`${path}/new`));
          break;
        case 'reply_file': // 回复(带附件)
          setNewParams(6, () => history.push(`${path}/new`));
          break;
        case 'ReplyAll_file': // 回复全部(带附件)
          setNewParams(7, () => history.push(`${path}/new`));
          break;
        default:
          break;
      }
    }
  }

  showActionSheet = (datas) => {
    let BUTTONS = datas.map(v => {
      return({
        content: v.label,
        onClick: () => this.onPress(v),
      });
    });
    BUTTONS.push({
      content: getLabel(201, '取消'),
    });
    ActionSheet.showActionSheetWithOptions({
      options: BUTTONS,
      cancelButtonIndex: BUTTONS.length - 1,
      maskClosable: true,
      'data-seed': 'logId',
      onTouchStart: e => e.preventDefault(),
    });
  }

  render() {
		const { prefixCls, data } = this.props;
    if (!data || data.length === 0) return (<div></div>);

    return (
      <div className={prefixCls}>
        <TabBar animated={false} useOnPan={false}>
          {
            data.map(d => (
              <TabBar.Item
                {...d}
                title={d.label}
                key={d.key}
                icon={<Icon type={d.icon} />}
                onPress={(...arg) => this.onPress(d)}
              />
            ))
          }
        </TabBar>
      </div>
    );
  }
}